var net="http://www.gssadtfw.com/";//域名

//document.write("<script language=javascript src='../static/js/jquery.md5.js'></script>");
//document.write("<script language=javascript src='../static/js/session.js'></script>");
//获取当前页面id参数
function getQueryString(name) {
  var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
  var r = window.location.search.substr(1).match(reg);
  if (r != null) return unescape(r[2]); return null;
}
function disableBtn(obj){
	$(obj).prop("disabled",true);
	setTimeout(function(){ $(obj).prop("disabled",false); }, 3000);
}
// 时间戳转为日期
function timestampToTime(timestamp) {
	var date = new Date(timestamp * 1000);//时间戳为10位需*1000，时间戳为13位的话不需乘1000
	var Y = date.getFullYear() + '-';
	var M = (date.getMonth()+1 < 10 ? '0'+(date.getMonth()+1) : date.getMonth()+1) + '-';
	var D = date.getDate() + ' ';
	var h = date.getHours() + ':';
	var m = date.getMinutes() ;
	if(m>=0&&m<10){
		m="0"+m;
	}
//var s = date.getSeconds();
	return Y+M+D+h+m;
}
// 时间戳转为日期
function timestampToTime1(timestamp) {
	var date = new Date(timestamp * 1000);//时间戳为10位需*1000，时间戳为13位的话不需乘1000
	var Y = date.getFullYear() + '-';
	var M = (date.getMonth()+1 < 10 ? '0'+(date.getMonth()+1) : date.getMonth()+1) + '-';
	var D = date.getDate() + ' ';
	var h = date.getHours() + ':';
	var m = date.getMinutes() + ':';
	var s = date.getSeconds();
	return Y+M+D+h+m+s;
}
// 时间戳转为时分
function timestampTohourse(timestamp) {
	var date = new Date(timestamp * 1000);//时间戳为10位需*1000，时间戳为13位的话不需乘1000
//	var Y = date.getFullYear() + '-';
//	var M = (date.getMonth()+1 < 10 ? '0'+(date.getMonth()+1) : date.getMonth()+1) + '-';
//	var D = date.getDate() + ' ';
	var h = date.getHours() + ':';
	var m = date.getMinutes();
//	var s = date.getSeconds();
	return h+m;
}
// 时间戳转为日期(不带时间)
function timestampToDate(timestamp) {
	var date = new Date(timestamp * 1000);//时间戳为10位需*1000，时间戳为13位的话不需乘1000
	var Y = date.getFullYear() + '-';
	var M = (date.getMonth()+1 < 10 ? '0'+(date.getMonth()+1) : date.getMonth()+1) + '-';
	var D = date.getDate() + ' ';
	return Y+M+D;
}
// 时间戳转为年月(不带时间)
function timestampToMonth(timestamp) {
	var date = new Date(timestamp * 1000);//时间戳为10位需*1000，时间戳为13位的话不需乘1000
	var Y = date.getFullYear() + '-';
	var M = (date.getMonth()+1 < 10 ? '0'+(date.getMonth()+1) : date.getMonth()+1);
	return Y+M;
}
// 时间戳转为年龄
function timestampToAge(timestamp){
  var now = new Date().getTime();
  now = parseInt(now/1000);
  var age = (now - parseInt(timestamp))/(356*24*60*60);
  return parseInt(age);
}
function http_request(param,callback){
  // 判断是否需要session
  // var session = localStorage.getItem("session");
  var session = getCookie("session");
  // if(session!=""&&session!=null) {param.session=session};
  // 判断参数是否为空
  if (param=={}||param==""||param==null) {
    callback({'code':1,'msg':"参数错误"});
    return false;
  }
  param.session = session;

  // 获取时间戳
  var timestamp = (new Date()).valueOf();
  timestamp = parseInt(timestamp/1000);
  param.timestamp = timestamp;
  param.app_key = "app_2";
  // 对参数进行排序
  param = ksort(param);
  // console.log(param);
  param.sign = generateSign(param);
  // console.log(param);
  $.ajax({
      type: "post",
      async: false,
      url: "http://www.lianshoumeili.xbtcsc.com.cn/huanianApi/user_index.php",
      data: param,
      dataType: "jsonp",
      jsonp: "callback",//传递给请求处理程序或页面的，用以获得jsonp回调函数名的参数名(一般默认为:callback)
      jsonpCallback:"flightHandler",//自定义的jsonp回调函数名称，默认为jQuery自动生成的随机函数名，也可以写"?"，jQuery会自动为你处理数据
      success: function(json){
        callback(json);
      },
      error: function(error){
        console.log(error);
        callback({'code':1,'msg':"网络连接错误"});
        return false;
      }
  });
}

function generateSign(data)
{
  var app_secret = "b20bf98c2b62398b3975a9a85b64e795";
  var sub_sign = "";
  data = ksort(data);
  $.each(data,function(index,value){
    sub_sign += index+value;
  })
  sub_sign = app_secret+sub_sign+app_secret;
  // console.log(sub_sign);
  //进行md5加密，并返回
  sub_sign = $.md5(sub_sign);
  return sub_sign;
}

function ksort(inputArr, sort_flags) {
  var tmp_arr = {},
    keys = [],
    sorter, i, k, that = this,
    strictForIn = false,
    populateArr = {};

  switch (sort_flags) {
  case 'SORT_STRING':
    // compare items as strings
    sorter = function (a, b) {
      return that.strnatcmp(a, b);
    };
    break;
  case 'SORT_LOCALE_STRING':
    // compare items as strings, original by the current locale (set with  i18n_loc_set_default() as of PHP6)
    var loc = this.i18n_loc_get_default();
    sorter = this.php_js.i18nLocales[loc].sorting;
    break;
  case 'SORT_NUMERIC':
    // compare items numerically
    sorter = function (a, b) {
      return ((a + 0) - (b + 0));
    };
    break;
    // case 'SORT_REGULAR': // compare items normally (don't change types)
  default:
    sorter = function (a, b) {
      var aFloat = parseFloat(a),
        bFloat = parseFloat(b),
        aNumeric = aFloat + '' === a,
        bNumeric = bFloat + '' === b;
      if (aNumeric && bNumeric) {
        return aFloat > bFloat ? 1 : aFloat < bFloat ? -1 : 0;
      } else if (aNumeric && !bNumeric) {
        return 1;
      } else if (!aNumeric && bNumeric) {
        return -1;
      }
      return a > b ? 1 : a < b ? -1 : 0;
    };
    break;
  }

  // Make a list of key names
  for (k in inputArr) {
    if (inputArr.hasOwnProperty(k)) {
      keys.push(k);
    }
  }
  keys.sort(sorter);

  // BEGIN REDUNDANT
  this.php_js = this.php_js || {};
  this.php_js.ini = this.php_js.ini || {};
  // END REDUNDANT
  strictForIn = this.php_js.ini['phpjs.strictForIn'] && this.php_js.ini['phpjs.strictForIn'].local_value && this.php_js
    .ini['phpjs.strictForIn'].local_value !== 'off';
  populateArr = strictForIn ? inputArr : populateArr;

  // Rebuild array with sorted key names
  for (i = 0; i < keys.length; i++) {
    k = keys[i];
    tmp_arr[k] = inputArr[k];
    if (strictForIn) {
      delete inputArr[k];
    }
  }
  for (i in tmp_arr) {
    if (tmp_arr.hasOwnProperty(i)) {
      populateArr[i] = tmp_arr[i];
    }
  }

  return strictForIn || populateArr;
}

//in_array()函数定义
function in_array(stringToSearch, arrayToSearch) {
  for ( var s = 0; s < arrayToSearch.length; s++) {
    thisEntry = arrayToSearch[s].toString();
    if (thisEntry == stringToSearch) {
      return true;
    }
  }
  return false;
}
